package com.mars.common.utils;

import com.ag.utils.CollectionUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description: 返回结果
 * @author: YUAN YONG
 * @create: 2019-07-11
 **/

public class R extends HashMap<String,Object> {


    public R() {
        put("code", 0 );
        put("msg", "操作成功");

    }

    public static R error() {
        return error(-1, "操作失败");
    }

    public static R operate(boolean b){
        if(b){
            return R.ok();
        }
        return R.error();
    }

    public static R error(String msg) {
        return error(-1, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }
    public static R ok(int code,String msg) {
        R r = new R();
        r.put("code",code);
        r.put("msg", msg);
        return r;
    }

    public static R ok() {
        return new R();
    }

    /**
     * Obj
     * @param data
     * @return
     */
    public static R data(Object data){
        return R.ok().put("data",data);
    }

    /**
     * 多个Map
     * @param maps
     * @return
     */
    public static R maps(Map... maps){

        if(CollectionUtils.isEmpty(maps)){
            return R.ok();
        }
        LinkedHashMap<String,Object> rMap = new LinkedHashMap<String,Object>();
        for(Map<String,Object> _map : maps){
           rMap.putAll(_map);
        }
        return R.ok().put("data",rMap);
    }

    /**
     * 分页对象
     * @param page
     * @return
     */
    @Deprecated
    public static R page(Object page){
        return R.ok().put("page",page);
    }

    /**
     * 不推荐用,用maps
     * @param page
     * @param map
     * @return
     */
    @Deprecated
    public static R page(Object page,Map map,Map map2){
        return R.ok().put("page",page).put("countComment",map).put("totalCommentNum",map2);
    }




    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
